# Maintainer: Matthias Gondan <Matthias.Gondan-Rochon@uibk.ac.at>

_realname=swi-prolog
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.0.4
pkgrel=3
pkgdesc="Prolog environment (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'mingw32' 'ucrt64' 'clang64' 'clang32')
url="https://www.swi-prolog.org/"
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-gmp: support for large integers"
            "${MINGW_PACKAGE_PREFIX}-libyaml: yaml support"
            "${MINGW_PACKAGE_PREFIX}-db: support for Berkeley db"
            "${MINGW_PACKAGE_PREFIX}-pcre2: support for regular expressions")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gmp"
             "${MINGW_PACKAGE_PREFIX}-libarchive"
             "${MINGW_PACKAGE_PREFIX}-libyaml"
             "${MINGW_PACKAGE_PREFIX}-db"
             "${MINGW_PACKAGE_PREFIX}-pcre2"
             "${MINGW_PACKAGE_PREFIX}-xpm-nox"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("https://www.swi-prolog.org/download/stable/src/swipl-${pkgver}.tar.gz"
        "01-test_pipe.patch"
        "02-unixdirs-cmakelist.patch"
        "03-unixdirs-src-cmakelist.patch"
        "04-unixdirs-boot-init.patch"
        "05-unixdirs-library-prolog_config.patch"
        "06-unixdirs-build-tools.patch"
        "07-test_uri.patch")

sha256sums=('feb2815a51d34fa81cb34e8149830405935a7e1d1c1950461239750baa8b49f0'
            'c412572542290b871a5f25fa088c96c09e778398f157322ecabee580b2a1be7b'
            '30d729e5ea584e4f7c134f5fac2bc53d61081b0dd1f9812ba3bb0941ebb50b22'
            'fbbc34b8a450c51839190eb7a0b3b43cd8a2ccb74e8fdbcde9ceb6ce60e61847'
            'a7f1c46606466e0084f497247abbad71afa606fb24f1a0ceeb33d106d1b74299'
            '5aa1172661f0ecfe2e90696dcbcb7632515ece0ede85fb67dda7f1d1cf5a0955'
            '1e8ee40e4139a70c49b86bfa5147455ab9af3b91dc2f0972c4791be0059e7b01'
            '43e5aeb9c48b0e03f2fe4b23c9f57377dba80b2731cfc5dcc51ca9886ce93fe7')

prepare() {
  cd ${srcdir}/swipl-${pkgver}
  patch -p1 < ${srcdir}/01-test_pipe.patch
  patch -p1 < ${srcdir}/02-unixdirs-cmakelist.patch
  patch -p1 < ${srcdir}/03-unixdirs-src-cmakelist.patch
  patch -p1 < ${srcdir}/04-unixdirs-boot-init.patch
  patch -p1 < ${srcdir}/05-unixdirs-library-prolog_config.patch
  patch -p1 < ${srcdir}/06-unixdirs-build-tools.patch
  patch -p1 < ${srcdir}/07-test_uri.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DMSYS2=1 \
    -DWIN32_DLLS="" \
    -DMINGW_ROOT="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    -DSWIPL_PACKAGES_JAVA=OFF \
    -DSWIPL_PACKAGES_X=OFF \
    -DINSTALL_DOCUMENTATION=OFF \
    -DSWIPL_{SHARED,STATIC}_LIB=ON \
    ../swipl-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  install -Dm644 "${srcdir}/swipl-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

